﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.Shell.Interop;
using EnvDTE;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.Platform.WindowManagement.DTE;
using VSIXUtilities.DataObjects.Enums;
using System.Diagnostics;

namespace VSIXUtilities.DataObjects
{
    [DebuggerDisplay("Caption = {Caption}, ShortCaption = {ShortCaption}")]
    public class IVsWindowFrameWrapper
    {
        public IVsWindowFrameWrapper(IVsWindowFrame IVsWindowFrameInstance)
		{
            if (IVsWindowFrameInstance == null)
            {
                throw new ArgumentException("\"WindowFrame\" can not be null.");
            }
            this.WindowFrame = IVsWindowFrameInstance;
		}

        public static IVsWindowFrameWrapper FromIVsWindowFrame(IVsWindowFrame IVsWindowFrameInstance)
        {
            return new IVsWindowFrameWrapper(IVsWindowFrameInstance);
        }
        public IVsWindowFrame WindowFrame
        {
            get;
            private set;
        }

        public IVsWindowFrameType Type
        {
            get
            {
                return (IVsWindowFrameType)GetProperty<int>(__VSFPROPID.VSFPROPID_Type);
            }
        }

        public object DocView
        {
            get
            {
                return GetProperty(__VSFPROPID.VSFPROPID_DocView);
            }
        }

        public Microsoft.VisualStudio.OLE.Interop.IServiceProvider SPFrame
        {
            get
            {
                return GetProperty<Microsoft.VisualStudio.OLE.Interop.IServiceProvider>(__VSFPROPID.VSFPROPID_SPFrame);
            }
        }

        public Microsoft.VisualStudio.OLE.Interop.IServiceProvider SPProjContext
        {
            get
            {
                return GetProperty<Microsoft.VisualStudio.OLE.Interop.IServiceProvider>(__VSFPROPID.VSFPROPID_SPProjContext);
            }
        }

        public string Caption
        {
            get
            {
                return GetProperty<string>(__VSFPROPID.VSFPROPID_Caption);
            }
        }

        public vsWindowState WindowState
        {
            get
            {
                return (vsWindowState)GetProperty<int>(__VSFPROPID.VSFPROPID_WindowState);
            }
        }

        public VSFRAMEMODE FrameMode
        {
            get
            {
                return (VSFRAMEMODE)GetProperty<int>(__VSFPROPID.VSFPROPID_FrameMode);
            }
        }

        public WindowTabState IsWindowTabbed
        {
            get
            {
                return (WindowTabState)GetProperty<short>(__VSFPROPID.VSFPROPID_IsWindowTabbed);
            }
        }

        public IVsUserContext UserContext
        {
            get
            {
                return GetProperty<IVsUserContext>(__VSFPROPID.VSFPROPID_UserContext);
            }
        }

        public object ViewHelper
        {
            get
            {
                return GetProperty(__VSFPROPID.VSFPROPID_ViewHelper);
            }
        }

        public string ShortCaption
        {
            get
            {
                return GetProperty<string>(__VSFPROPID.VSFPROPID_ShortCaption);
            }
        }

        public string WindowHelpKeyword
        {
            get
            {
                return GetProperty<string>(__VSFPROPID.VSFPROPID_WindowHelpKeyword);
            }
        }

        public string WindowHelpCmdText
        {
            get
            {
                return GetProperty<string>(__VSFPROPID.VSFPROPID_WindowHelpCmdText);
            }
        }

        public int DocCookie
        {
            get
            {
                return GetProperty<int>(__VSFPROPID.VSFPROPID_DocCookie);
            }
        }

        public string OwnerCaption
        {
            get
            {
                return GetProperty<string>(__VSFPROPID.VSFPROPID_OwnerCaption);
            }
        }

        public string EditorCaption
        {
            get
            {
                return GetProperty<string>(__VSFPROPID.VSFPROPID_EditorCaption);
            }
        }

        public string MkDocument
        {
            get
            {
                return GetProperty<string>(__VSFPROPID.VSFPROPID_pszMkDocument);
            }
        }

        public IVsTextBuffer DocData
        {
            get
            {
                return GetProperty<IVsTextBuffer>(__VSFPROPID.VSFPROPID_DocData);
            }
        }

        public IVsHierarchy Hierarchy
        {
            get
            {
                return GetProperty<IVsHierarchy>(__VSFPROPID.VSFPROPID_Hierarchy);
            }
        }

        public int ItemID
        {
            get
            {
                return GetProperty<int>(__VSFPROPID.VSFPROPID_ItemID);
            }
        }

        public Guid CmdUIGuid
        {
            get
            {
                return GetProperty<Guid>(__VSFPROPID.VSFPROPID_CmdUIGuid);
            }
        }

        public __VSCREATEDOCWIN CreateDocWinFlags
        {
            get
            {
                return GetProperty<__VSCREATEDOCWIN>(__VSFPROPID.VSFPROPID_CreateDocWinFlags);
            }
        }

        public Guid GuidEditorType
        {
            get
            {
                return GetProperty<Guid>(__VSFPROPID.VSFPROPID_guidEditorType);
            }
        }

        public string PhysicalView
        {
            get
            {
                return GetProperty<string>(__VSFPROPID.VSFPROPID_pszPhysicalView);
            }
        }

        public Guid InheritKeyBindings
        {
            get
            {
                return GetProperty<Guid>(__VSFPROPID.VSFPROPID_InheritKeyBindings);
            }
        }

        public IVsTextBuffer RDTDocData
        {
            get
            {
                return GetProperty<IVsTextBuffer>(__VSFPROPID.VSFPROPID_RDTDocData);
            }
        }

        public IVsTextBuffer AltDocData
        {
            get
            {
                return GetProperty<IVsTextBuffer>(__VSFPROPID.VSFPROPID_AltDocData);
            }
        }

        public Guid GuidPersistenceSlot
        {
            get
            {
                return GetProperty<Guid>(__VSFPROPID.VSFPROPID_GuidPersistenceSlot);
            }
        }

        public __VSCREATETOOLWIN CreateToolWinFlags
        {
            get
            {
                return GetProperty<__VSCREATETOOLWIN>(__VSFPROPID.VSFPROPID_CreateToolWinFlags);
            }
        }

        public WindowBase ExtWindowObject
        {
            get
            {
                return GetProperty<WindowBase>(__VSFPROPID.VSFPROPID_ExtWindowObject);
            }
        }

        public uint MultiInstanceToolNum
        {
            get
            {
                return GetProperty<uint>(__VSFPROPID.VSFPROPID_MultiInstanceToolNum);
            }
        }

        public int BitmapResource
        {
            get
            {
                return GetProperty<int>(__VSFPROPID.VSFPROPID_BitmapResource);
            }
        }

        public int BitmapIndex
        {
            get
            {
                return GetProperty<int>(__VSFPROPID.VSFPROPID_BitmapIndex);
            }
        }

        public object ToolbarHost
        {
            get
            {
                return GetProperty(__VSFPROPID.VSFPROPID_ToolbarHost);
            }
        }

        private object GetProperty(__VSFPROPID PropertyId)
        {
            object pvar;
            WindowFrame.GetProperty(Convert.ToInt32(PropertyId), out pvar);
            return pvar;
        }

        private T GetProperty<T>(__VSFPROPID PropertyId)
        {
            object Result = GetProperty(PropertyId);
            return (Result == null) ? default(T) : (T)Result;
        }
    }
}
